<?php
namespace app\common\model\logic;

use think\Model;

class CarServiceModel extends Model {

    protected $name = 'car_service'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    //类型自动转换
    protected $type = [
        'apply_uid'=>'integer',
        'car_id'=>'integer',
        'repair_uid'=>'integer',
        'ser_start_time'=>'timestamp:Y-m-d H:i:s',
        'ser_end_time'=>'timestamp:Y-m-d H:i:s',
        'service_money'=>'integer',
        'sort'=>'integer',
        'ser_desc'=>'array'
    ];
    public function getSerTypeTextAttr($value, $data)
    {
        $arr=['0'=>'维修','1'=>'保养'];
        return $arr[$data['ser_type']];
    }
    public function getServiceStatusTextAttr($value, $data)
    {
        $arr=['0'=>'已提交','1'=>'开始处理','2'=>'处理完成'];
        return $arr[$data['service_status']];
    }


    /**
     * 获取检索条件
     * @param $query
     */
    public function getExtendMap($query){
        $map=[];
        if($query['service_status']>-1){
            $map['service_status']=$query['service_status'];
        }
        if(!empty($query['car_sn'])){
            $map['car_sn']=['like','%'.$query['car_sn'].'%'];
        }
        if(!empty($query['parts_text'])){
            $map['ser_desc']=['like','%'.$query['parts_text'].'%'];
        }
        $rangdate=$query["rangdate"];
        $rangdatearr = explode('~',$rangdate);
        $begin = strtotime($rangdatearr[0].' 0:00:00');
        $end = strtotime($rangdatearr[1].' 23:59:59');
        if(!empty($rangdate)){
            $map['ser_start_time']=['between',[$begin,$end]];
        }
        return $map;
    }

    //读取单条数据
    public function read($aid){
        try{
            $info = $this->get($aid)->toArray();
            return $info;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    
    //格式化列表
    public function formatList($list){
        return $list;
        dump($list);
    }
    //格式化单条内容
    public function formatInfo($info){
        return $info;
    }
    
    //添加数据
    public function add($data){
        try{
            $this->isUpdate(false)->allowField(true)->save($data);
            return true;
        }catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    //修改数据
    public function edit($data){
        try{

            $this->isUpdate(true)->allowField(true)->save($data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    //删除数据
    public function del($aid){
        try{
            $this->destroy($aid);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
}